import { ref, onMounted, onBeforeUnmount } from 'vue'
import { getFilmsApi } from '@/api/filmApi'

export const useNowPlayingListHook = () => {
  const data = ref([])
  const total = ref(0)
  const pageNum = ref(1)
  const loadData = async page => {
    page = page ?? pageNum.value
    let ret = await getFilmsApi(page)
    if (ret.films.length > 0) {
      data.value = [...data.value, ...ret.films]
      total.value = ret.total
      pageNum.value++
    }
    return true
  }
  return [data, loadData, total]
}

export const useScrollHook = () => {
  const scrollFn = () => {
    console.log('滚动了')
  }

  onMounted(() => {
    window.addEventListener('scroll', scrollFn)
  })
  onBeforeUnmount(() => {
    window.removeEventListener('scroll', scrollFn)
  })
}
